<?php

namespace App\Http\Controllers\Admin\ly;

use App\Common\Paginate;
use App\Http\Controllers\Controller;
use App\Utils\Logic\Ly\Agentlogic;
use Illuminate\Http\Request;
use App\Common\Responses;
use App\Models\Agent;
class AgentController extends Controller
{
    /**
     * 经纪人列表
     */
    public function index(Request $request)
    {
        try {
              $params['page']=(int)$request->get('page');//当前页
              $params['limit']=(int)$request->get('limit');//条数
              $params['search']=$request->get('search');
              $params['status']=$request->get('status');
              list($limit,$offset) =Paginate::paginate( $params['page'], $params['limit']);
              $data=Agent::Getpaginate($params,$limit,$offset);
              $data['page'] = (int)$params['page'];
              return Responses::success($data,20000,'success');
        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }
    }
    /**
     * 经纪人详情
     */
    public function detail(Request $request)
    {
        $id=$request->get('id');
        try {
            $data=Agentlogic::detail($id);
            return Responses::success($data);
        }catch (\Exception $e)
        {
            return Responses::error([],0,$e->getMessage());
        }
    }

    /**
     * 经纪人添加
     * gao
     * @param Request $request
     * @return false|string
     */
    public function create(Request $request){
        try {
            $params = $request->validate([
                'name' => 'required',
                'phone' => 'required',
                'company' => 'required',
            ]);
            $result = Agent::border($params);
            if (!$result) throw new \Exception('未知错误，添加失败');
            return Responses::success($params,20000,'success');
        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }
    }
    /**
     * 经纪人修改
     * gao
     * @param Request $request
     * @return false|string
     */
    public function update(Request $request){
        try {
            $id = $request->post('id');
            $params = $request->validate([
                'name' => 'required',
                'phone' => 'required',
                'company' => 'required',
            ]);
            $result = Agent::updateBroker($id,$params);
            if (!$result) throw new \Exception('未知错误，修改失败');
            return Responses::success($params);
        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }
    }

    /**
     * 经纪人删除
     * gao
     * @param Request $request
     * @return false|string
     */
    public function del(Request $request){
        try {
            $id = $request->get('id');
            $result = Agent::deleteBroker($id);
            if (!$result) throw new \Exception('未知错误，删除失败');
            return Responses::success($result);
        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }
    }
}
